package mylist
{
	import com.ViewSet;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class IconItem extends Sprite
	{
		public static var selectIndex:int;
		public static var ww:int = 200;
		public static var  hh:int = 200;
		public var index:int;
		private var fileSize:uint;
		private static const wBorder:int = 3;
		private static const hBorder:int = 3;
		
		private var grayMask:Bitmap;
		private var greenProgress:Bitmap;
		private var progressTxt:TextField = new TextField();
		
		public function IconItem(bmp:Bitmap)
		{
			var txtBg:TextField = new TextField();
			addChild(txtBg);
			txtBg.width = ww;
			txtBg.height = hh;
			txtBg.alpha = 0;
			ViewSet.fullCenter(bmp,ww*.05,hh*.05,ww*.9,hh*.9);
			addChild(bmp);
			mouseChildren = false;
			addEventListener(Event.ADDED_TO_STAGE, init);
			
			if (DragItem.willSend) {
				progressTxt = new TextField();
				progressTxt.border = true;
				progressTxt.filters = [new GlowFilter()];
				progressTxt.width = ww;
				progressTxt.height = hh;
				//progressTxt.defaultTextFormat = new TextFormat(null,22);
				grayMask = new Bitmap(new BitmapData(ww,hh,true,0x33cccccc));
				greenProgress = new Bitmap(new BitmapData(ww,hh,true,0x3300cc00));
				addChild(grayMask);
				addChild(greenProgress);
				addChild(progressTxt);
				setProgress(.0);
			}
		}
		
		public function setProgress(rate:Number):void
		{
			if (rate == 1) {
				grayMask.visible = false;
				greenProgress.visible = false;
				progressTxt.visible = false;
			}else if(rate>=0 && rate < 1){
				greenProgress.height = ww * rate;
				greenProgress.y = ww -greenProgress.height;
				progressTxt.text = int(rate * 100) + "%";
			}else {
				progressTxt.textColor = 0xff0000;
				progressTxt.text = "网络出错！";
				greenProgress.height = 0;
			}
		}
		
		public function setIndex(i:int):void
		{
			index = i;
			name = "c" + index;
		}
		
		private function init(e:Event):void
		{
			showthis();
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			addEventListener(MouseEvent.MOUSE_UP, clicked);
			addEventListener(MouseEvent.MOUSE_DOWN, clicked);
			addEventListener(MouseEvent.MOUSE_OUT, clicked);
		
		}
		
		private function clearTimeouts(e:MouseEvent):void
		{
			PressTimeOut.close();
			stage.removeEventListener(MouseEvent.MOUSE_UP, clearTimeouts);
			DragItem.main.backToList(null);
		}
		
		private static var mouY:int;
		
		private function clicked(e:MouseEvent):void
		{
			if (stage == null) return;
				switch (e.type)
			{
				case MouseEvent.MOUSE_UP: 
					if (DragItem.willSend)
						PressTimeOut.close();
					if (Math.abs(mouY - stage.mouseY) > 5)
						return;
					showBig(index);
					break;
				case MouseEvent.MOUSE_DOWN: 
					selectIndex = index;
					mouY = stage.mouseY;
					if (!DragItem.willSend)
						return;
					PressTimeOut.start(this);
					this.addEventListener(PressTimeOut.COMPLETE, startDrags);
					stage.addEventListener(MouseEvent.MOUSE_UP, clearTimeouts);
					//if (FileItem.list[index].state == FileItem.UPLOADED || FileItem.list[index].state == FileItem.UPLOADING)
					startSend();
					break;
				case MouseEvent.MOUSE_OUT: 
				case MouseEvent.ROLL_OVER: 
					mouY = int.MAX_VALUE;
					break;
				default: 
			}
		}
		
		public function startSend():void
		{
			clearTimeout(timeOutId);
			if (FileItem.list[index].state == FileItem.ADDED)
			{
				FileItem.list[index].state = FileItem.UPLOADING;
				FileItem.list[index].time = uint(Number(new Date()) / 1000);
				timeOutId = setTimeout(MySocket.sendBmp,100,FileItem.list[index].time, index, FileItem.list[index].bmp, sended, sendBmpError);
				
				//MySocket.sendBmp(FileItem.list[index].time, index, FileItem.list[index].bmp, ViewList.main.sended, ViewList.main.sendBmpError);
			}
			
		}
		
		
		private  function sendBmpError(e:Event):void 
		{
			if (FileItem.list[index].state == FileItem.UPLOADING)
				FileItem.list[index].state = FileItem.ADDED;
				
				setProgress(-1);
			//startSend();
		}
		private function sended(e:Event):void
		{
			if (FileItem.list[index].state == FileItem.UPLOADING)
				FileItem.list[index].state = FileItem.UPLOADED;
		}
		
		private var timeOutId:uint;
		
		/**
		 * 全屏大图
		 * @param	_index
		 */
		public static function showBig(_index:int):void
		{
			ViewList.main.stage.addChild(ShowBig.main);
			ShowBig.main.hide();
			ShowBig.main.shows(FileItem.list[_index].bmp);
		}
		
		private function startDrags(e:Event):void
		{
			this.removeEventListener(PressTimeOut.COMPLETE, startDrags);
			var fileItem:FileItem = FileItem.list[index];
			stage.removeEventListener(MouseEvent.MOUSE_UP, clearTimeouts);
			//logs.adds("timeOut");
			//clearTimeout(timeOutId);
			//if (Math.abs(mouY - stage.mouseY) > 5)
			//return;
			
			//fileItem.state = FileItem.DRAG;
			if (DragItem.willSend)
				DragItem.main.show(fileItem);
			//else
			//fileItem.state = FileItem.UPLOADED;
		}
		
		/**
		 * 移到遮罩显示的范围内
		 */
		public function showthis():void
		{
			if (parent.mask)
			{
				while (localToGlobal(new Point(0, 0)).y + height > ViewList.origY + parent.mask.height)
					parent.y -= height;
				while (localToGlobal(new Point(0, 0)).y < ViewList.origY - 2)
					parent.y += height;
					//ScrollBar.main.setShow();
			}
		}
		
		private function Base64Type(bmpd:BitmapData):void
		{
			var encoder:JPGEncoder = new JPGEncoder(80);
			var bytes:ByteArray = encoder.encode(bmpd);
		}
	
	}

}